package live.zs.securitytokenlocal.controller;

import live.zs.securitytokenlocal.config.CustomAuthManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author madison
 * @description
 * @date 2021/7/15 20:13
 */
@RestController
@RequestMapping("/auth")
public class SecurityController {

    @Autowired
    private CustomAuthManager authManager;

    @GetMapping("login")
    public String loginPage(HttpServletRequest request){
        return authManager.login(request);
    }

    @GetMapping("user")
    @ResponseBody
    public Map user(Principal principal) {
        Map<String, Object> map = new HashMap<>();
        map.put("context", SecurityContextHolder.getContext());
        map.put("principal", principal);
        map.put("user", SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        return map;
    }
}
